package com.jeecg.graduation;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jeecg.common.Assert;
import com.jeecg.student.entity.GStudentEntity;
import com.jeecg.student.service.GStudentServiceI;
import com.jeecg.teacher.entity.GTeacherEntity;
import com.jeecg.teacher.service.GTeacherServiceI;

/**
 * 会员相关
 * 
 * @author Debenson
 * @since 0.1
 */
@Controller
@RequestMapping("/graduation/mbr")
public class MemberController extends SBaseController {
  private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

  @Autowired
  private SystemService systemService;
  
  @Autowired
  private GStudentServiceI studentServiceI;
  
  @Autowired
  private GTeacherServiceI teacherServiceI;
  
  
  /**
   * 显示登录界面
   * 
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "login", method = RequestMethod.GET)
  public ModelAndView showLogin() throws Exception {
    return showView("mbr/login");
  }
  /**
   * 显示首页界面
   * 
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "index", method = RequestMethod.GET)
  public ModelAndView showindex() throws Exception {
	  return showView("mbr/index2");
  }
  @RequestMapping(value = "leave")
  public ModelAndView showLeave(HttpServletRequest request) throws Exception {
    return showView("leave/leaveList");
  }
  @RequestMapping(value = "leave2")
  public ModelAndView showLeave2(HttpServletRequest request) throws Exception {
	  return showView("leave2/leave2List");
  }
  
  
  @RequestMapping(value = "myStudent", method = RequestMethod.GET)
  public ModelAndView myStudent() throws Exception {
	  return showView("myStudent/myStudentList");
  }
  @RequestMapping(value = "myStudent1", method = RequestMethod.GET)
  public ModelAndView myStudent1() throws Exception {
	  return showView("attend/myStudentList");
  }
  @RequestMapping(value = "myTeacher", method = RequestMethod.GET)
  public ModelAndView myTeacher() throws Exception {
	  return showView("myTeacher/myTeacherList");
  }
  @RequestMapping(value = "changePassword", method = RequestMethod.GET)
  public ModelAndView changePassword() throws Exception {
	  return showView("changePassword/changePasswordEdit");
  }
  @RequestMapping(value = "changePassword2", method = RequestMethod.GET)
  public ModelAndView changePassword2() throws Exception {
	  return showView("changePassword/changePasswordEdit2");
  }
  @RequestMapping(value = "myClass", method = RequestMethod.GET)
  public ModelAndView myClass() throws Exception {
	  return showView("myClass/studentList");
  }
  @RequestMapping(value = "attend", method = RequestMethod.GET)
  public ModelAndView attend() throws Exception {
	  return showView("attend/attendList");
  }

  /**
   * 会员登录
   * 
   * @param lr
   * @param request
   * @return
   * @throws Exception
   */

  @ResponseBody
  @RequestMapping(value = "login", method = RequestMethod.POST)
  public AjaxJson login(@RequestBody LoginRequest lr) throws Exception {
	  if(lr.getJob().equalsIgnoreCase("student")){
		  GStudentEntity stu = studentServiceI.check(lr.getName(), lr.getPassword());
		  return AjaxJson.ok(stu);
	  }
	  if(lr.getJob().equalsIgnoreCase("teacher")){
		  GTeacherEntity stu = teacherServiceI.check(lr.getName(), lr.getPassword());
		  return AjaxJson.ok(stu);
	  }
	return null;
  }
  /**
   * 显示会员中心
   * 
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "center")
  public ModelAndView showCenter(HttpServletRequest request) throws Exception {
    String id = request.getParameter("id");
    String job = request.getParameter("job");
    Assert.assertArgumentNotNull(id, "id");
    Assert.assertArgumentNotNull(job, "job");
    
    if(job.equalsIgnoreCase("student")){
    	GStudentEntity stu = studentServiceI.getStudent(id);
    	request.setAttribute("job",true);
    	request.setAttribute("stu",stu);
    }else{
    	GTeacherEntity tea = teacherServiceI.getTeacher(id);
    	request.setAttribute("job",false);
    	request.setAttribute("stu",tea);

    }
    

    String signStr = "";
    // 进入会员中心之前发送异步消息 同步积分
    return showView("mbr/index");
  }
  
  /**
   * 登录请求
   * 
   * @author Debenson
   * @since 0.1
   */
  public static class LoginRequest implements Serializable {
    private static final long serialVersionUID = 6182066536898822140L;

    private String name;
    private String password;
    private String job;
    
    
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}


  }

}
